package utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Utils {

	public static String[] splitLine(String line, String delimiter) {
		if(delimiter.equals("|")){
			delimiter = "\\|";
		}
		
		if(delimiter.equals(".")){
			delimiter = "\\.";
		}
		
		String[] rtn = line.split(delimiter);
		for(int i=0;i<rtn.length;i++){
			rtn[i] = rtn[i].trim();
		}
		
		return rtn;
	}

	public static String[][] readlines(String dataFile) {
		return readAndSkipLines(dataFile, 0);
	}

	/**
	 * reads the lines from <code>dataFile</code>, ignoring the first 
	 * <code>toSkip</code> lines.
	 * 
	 * @param dataFile -- the filename of the file to read
	 * @param toSkip -- the number of lines to Skip
	 * @return -- a 2D array of Strings representing the file lines.
	 */
	public static String[][] readAndSkipLines(String dataFile, int toSkip) {
		List<String[]> lines = new ArrayList<String[]>();
		String line = null;
		
		
		try{
			FileReader fr = new FileReader(new File(dataFile));
			BufferedReader br = new BufferedReader(fr);
			
			while((line = br.readLine()) != null){
				lines.add(Utils.splitLine(line, "|"));
			}
			
		}catch(IOException e){
			e.printStackTrace();
		}
		
		for(int i=0;i<toSkip;i++){
			lines.remove(i);
		}
		
		String[][] linesArray = new String[lines.size()][];
		lines.toArray(linesArray);
		return linesArray;
	}
	
	public static List<String[]> readAndSkipLinesList(String dataFile, int toSkip) {
		List<String[]> lines = new ArrayList<String[]>();
		String line = null;
		
		
		try{
			FileReader fr = new FileReader(new File(dataFile));
			BufferedReader br = new BufferedReader(fr);
			
			while((line = br.readLine()) != null){
				lines.add(Utils.splitLine(line, "|"));
			}
			
		}catch(IOException e){
			e.printStackTrace();
		}
		
		for(int i=0;i<toSkip;i++){
			lines.remove(i);
		}
		
		return lines;
	}
	
	public static List<String[]> readLinesList(String dataFile){
		return readAndSkipLinesList(dataFile, 0);
	}

	public static  String[][] transposeArray(String[][] input) {
		int inRows = input.length;
		int inCols = input[0].length;
		
		String[][] rtn = new String[inCols][inRows];
		for(int i=0;i<inRows;i++){
			for(int j=0;j<inCols;j++){
				rtn[j][i] = input[i][j];
			}
		}
		
		return rtn;
	}
	
	public static  double[][] transposeArray(double[][] input) {
		int inRows = input.length;
		int inCols = input[0].length;
		
		double[][] rtn = new double[inCols][inRows];
		for(int i=0;i<inRows;i++){
			for(int j=0;j<inCols;j++){
				rtn[j][i] = input[i][j];
			}
		}
		
		return rtn;
	}

	public static List<String> readFile(String dataFile) {
		List<String> lines = new ArrayList<String>();
		String line = null;
		
		
		try{
			FileReader fr = new FileReader(new File(dataFile));
			BufferedReader br = new BufferedReader(fr);
			
			while((line = br.readLine()) != null){
				lines.add(line);
			}
			
		}catch(IOException e){
			e.printStackTrace();
		}
		
		return lines;
	}

	public static String[] getFileHeader(String dataFile) {
		return Utils.readlines(dataFile)[0];
	}

	public static double[] convertToDoubles(String[] input) {
		double[] output = new double[input.length];
		for(int i=0;i<output.length;i++){
			output[i] = Double.parseDouble(input[i]);
		}
		return output;
	}

	public static double[][] convertToDoubles(String[][] input) {
		int nRows = input.length;
		double[][] output = new double[nRows][];
		for(int i=0;i<nRows;i++){
			double[] temp = new double[input[i].length];
			for(int j=0;j<temp.length;j++){
				temp[j] = Double.parseDouble(input[i][j]); 
			}
			output[i] = temp;
		}
		return output;
	}

	public static double[][] dropColumn(double[][] input, int colNum) {
		if(colNum<=0 || colNum>input.length){
			return null;
		}
		
		List<double[]> colsToKeep = new ArrayList<double[]>(Arrays.asList(input));
		colsToKeep.remove(colNum - 1);
		double[][] rtn = new double[colsToKeep.size()][];
		colsToKeep.toArray(rtn);
		return rtn;
	}
}
